<template>
	<view></view>
</template>

<script>
	export default {
		data() {
			return {
				/*需要返回的图片*/
				fileList: []
			};
		},
		onLoad() {},
		props: ['num','type'],
		mounted() {
			if(this.$props.type == 'uploadFile'){
				this.chooseFileFunc();
			}else if(this.$props.type == 'uploadAiFile'){
				this.chooseFileFunc();
			}else{
				this.chooseImageFunc();
			}
		},
		methods: {
			/*打开相机或者相册，选择图片*/
			chooseImageFunc() {
				let self = this;
				uni.chooseImage({
					count: self.$props.num || 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function(res) {
						self.uploadFile(res.tempFilePaths);
					},
					fail: function(res) {
						self.$emit('getImgs', null);
					},
					complete: function(res) {
						console.log(res, '状态');
					}
				});
			},
			/*打开相机或者相册，选择文件*/
			chooseFileFunc() {
				let self = this;
				uni.chooseFile({
					count: self.$props.num || 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: function(res) {
						if(self.$props.type == 'uploadAiFile'){
							self.uploadAiFile(res.tempFiles);
						}else{
							self.uploadFile(res.tempFilePaths);
						}
					},
					fail: function(res) {
						self.$emit('getImgs', null);
					},
					complete: function(res) {
						console.log(res, '状态');
					}
				});
			},
			/*上传图片*/
			uploadFile: function(tempList) {
				let self = this;
				let i = 0;
				let img_length = tempList.length;
				let params = {
					token: uni.getStorageSync('token'),
					appId: self.getAppId()
				};
				uni.showLoading({
					title: '图片上传中'
				});
				tempList.forEach(function(filePath, fileKey) {
					uni.uploadFile({
						url: self.websiteUrl + '/api/front/file/upload/image',
						filePath: filePath,
						name: 'iFile',
						formData: params,
						header: {
							'appId': self.getAppId()
						},
						success: function(res) {
							let result = typeof res.data === 'object' ? res.data : JSON.parse(res
								.data);
							if (result.code === 1) {
								self.fileList.push(result.data);
							} else {
								self.showError(result.msg);
							}
						},
						complete: function() {
							i++;
							if (img_length === i) {
								uni.hideLoading();
								// 所有文件上传完成后调用处理方法
								if(self.$props.type == 'file'){
									self.$emit('getFiles', self.fileList);
								}else{
									self.$emit('getImgs', self.fileList);
								}
							}
						}
					});
				});
			},
			/*上传图片*/
			uploadAiFile: function(tempFiles) {
				let self = this;
				let i = 0;
				let img_length = tempFiles.length;
				let params = {
					token: uni.getStorageSync('token'),
					appId: self.getAppId()
				};
				uni.showLoading({
					title: '图片上传中'
				});
				tempFiles.forEach(function(tempFile, fileKey) {
					uni.uploadFile({
						url: self.websiteUrl + '/api/ai/text/uploadFile',
						filePath: tempFile.path,
						name: 'file',
						formData: params,
						header: {
							'appId': self.getAppId()
						},
						success: function(res) {
							let result = typeof res.data === 'object' ? res.data : JSON.parse(res
								.data);
							if (result.code === 1) {
								self.fileList.push(result.data);
							} else {
								self.showError(result.msg);
							}
						},
						complete: function() {
							i++;
							if (img_length === i) {
								uni.hideLoading();
								// 所有文件上传完成后调用处理方法
								if(self.$props.type == 'uploadAiFile'){
									self.$emit('getFiles', self.fileList,tempFile);
								}else{
									self.$emit('getImgs', self.fileList);
								}
							}
						}
					});
				});
			}
		}
	};
</script>

<style></style>